package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.cache.prefix.UserInfoKeyPrefix;
import cn.tedu.boyou.cache.service.IUserInfoRedisService;
import cn.tedu.boyou.common.util.AjaxResult;
import cn.tedu.boyou.member.service.IUserInfoService;
import cn.tedu.boyou.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//0412
import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;

@Controller
public class LoginController {
	
    @Autowired
    private RedisTemplate redisTemplate;
    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @RequestMapping("/checkPhone")
    @ResponseBody
    public boolean checkPhone(String phone){

        return !userInfoService.checkPhone(phone);
    }
    @RequestMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin(HttpSession session, HttpServletResponse response, String username, String password){
        String token = userInfoService.userLogin(username, password);
        CookieUtil.addCookie(response,UserInfoKeyPrefix.TOKEN_COOKIE_NAME,token);
        //因为登录成功之后信息放在redis,所以登录成功返回页面上，没有登录用户数据，需要额外指定
        session.setAttribute("userInfo",userInfoRedisService.getUserInfoByToken(UserInfoKeyPrefix.USER_INFO_TOKEN,token));

        return new AjaxResult();
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public AjaxResult regist(String phone, String nickname, String password, String rpassword, String verifyCode){
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
        return new AjaxResult();
    }
    
    /**
    * 实现用户的退出操作
    * 1.先查询 cookie 中的数据
    * 2.删除 redis
    * 3.删除 Cookie 信息
    * 4.重定向到系统首页
    */
    @RequestMapping("/logout")
    public String logout(HttpSession session,HttpServletRequest 
    request,HttpServletResponse response) {
    Cookie[] cookies = request.getCookies();
    //判断 cookie 是否为 null
    if(cookies == null || cookies.length==0 ) {
    return "redirect:/"; }
    String ticket = null; //初始化 ticket
    for (Cookie cookie : cookies) {
    if("JT_TICKET".equals(cookie.getName())) {
    ticket = cookie.getValue();
    break; } }
    
    //2.判断 ticket 是否有值
//    if(StringUtils.isEmpty(ticket)) {
//    return "redirect:/"; }
    //3.ticket 数据有效 删除 redis 和 cookie 数据
    //session.setAttribute("userInfo",null);
    //redisTemplate.delete(ticket);
    //jedisCluster.del(ticket);
    Cookie cookie = new Cookie("JT_TICKET", "");
    cookie.setMaxAge(0);
    cookie.setPath("/");
    cookie.setDomain("jt.com");
    response.addCookie(cookie);
    return "redirect:"; }
}
